Skip to content

Enhancements to Authentication#toBuilder#18050

Merged
jzheaux merged 3 commits into
spring-projects:mainfrom
jzheaux:builder-enhancements
Oct 15, 2025
Merged

Enhancements to Authentication#toBuilder#18050
jzheaux merged 3 commits into
spring-projects:mainfrom
jzheaux:builder-enhancements

Conversation

@jzheaux
Copy link
Copy Markdown
Collaborator

@jzheaux jzheaux commented Oct 14, 2025

This PR moves Authentication#toBuilder to a new sub-interface, BuildableAuthentication. This will improve passivity in the Security 7.0 release for applications that are using custom authentication implementations and relying on those concrete types.

Specifically, instead of doing:

Authentication merged = result.toBuilder().authorities((a) -> a.addAll(current.getAuthorities())).build();

You can do:

if (result instanceof BuildableAuthentication buildable) {
    Authentication merged = buildable.toBuilder().authorities((a) -> a.addAll(current.getAuthorities())).build();
}

Additionally, the PR adds authentication to BuilderAuthentication.Builder for easily application of common merging logic:

if (result instanceof BuildableAuthentication buildable) {
    Authentication merged = buildable.toBuilder().authentication(current).build();
}

@jzheaux jzheaux added this to the 7.0.0-RC1 milestone Oct 14, 2025
@jzheaux jzheaux self-assigned this Oct 14, 2025
Comment thread core/src/main/java/org/springframework/security/core/BuildableAuthentication.java Outdated
This commit consolidates logic common to applying one
authenticaiton to another. Specifically, it will copy the
authorities in one authentication into the builder instance
of another.

Closes spring-projectsgh-18053
Leaving the Builder in Authentication allows
authentication implementations to implement Builder
without needing to implement BuildableAuthentication.

Issue spring-projectsgh-18052
@jzheaux jzheaux force-pushed the builder-enhancements branch from d215dc5 to 21ff768 Compare October 15, 2025 18:01
@jzheaux jzheaux merged commit 95644fb into spring-projects:main Oct 15, 2025
3 of 4 checks passed
@jzheaux jzheaux removed this from the 7.0.0-RC1 milestone Oct 15, 2025
@jzheaux jzheaux deleted the builder-enhancements branch October 15, 2025 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants